
var _oServiceRetrieveCommand;
var _bFromSchedulingDialog = false;
function ScheduleActivity(sId, iObjectTypeCode, bAutoSearch)
{

if (!IsOnline())
{
alert(LOCID_GO_ONLINE_TO_SCHEDULE);
return;
}
var oWindowInfo = GetWindowInformation(ActivityScheduling);
var sUrl = prependOrgName("/") + oWindowInfo.Url + "?ActivityId=" + sId + "&ActivityTypeCode=" + iObjectTypeCode;
var iX	= oWindowInfo.Width;
var iY	= oWindowInfo.Height;
if (bAutoSearch)
{
sUrl += "&autoSearch=true";
}
var oActivity = BuildActivityInfo();
var oProposal = openStdDlg(sUrl, oActivity, iX, iY, true);
PopulateFromDialog(oProposal);
}
function BuildActivityInfo()
{
var i;
var item;
var oService = null;
var oSite = null;

var oServiceLookup = document.getElementById("serviceid");
var aoItems = oServiceLookup.Items();

if (aoItems.length == 1)
{
oService = new ServiceObject(aoItems[0].name, aoItems[0].id);
}
var oSiteLookup = document.getElementById("siteid");
if (oSiteLookup != null)
{
aoItems = oSiteLookup.Items();

if (aoItems.length == 1)
{
oSite = new SiteObject(aoItems[0].name, aoItems[0].id);
}
}
var aoResources = new Array();
var oAttendeeLookup = document.getElementById("resources");
aoItems = oAttendeeLookup.Items();
for (i = 0; i < aoItems.length; i++)
{
item = aoItems[i];
aoResources.push(new AttendeeObject(item.name, item.id, item.type, item.resourceSpecId, item.effort));
}
var aoCustomers = new Array();
oAttendeeLookup = document.getElementById("customers");
if (oAttendeeLookup != null)
{
aoItems = oAttendeeLookup.Items();
for (i = 0; i < aoItems.length; i++)
{
item = aoItems[i];
aoCustomers.push(new AttendeeObject(item.name, item.id, item.type));
}
}
return new ActivityInfo(aoCustomers, aoResources, oService, oSite);
}

function PopulateFromDialog(oActivity)
{

if (oActivity == null)
{
return;
}
var aoItems;
var oLookup;
var oLookupItem;
var i;
var oService;
var oSite;
var oAttendee;

oLookup = document.getElementById("serviceid");
oService = oActivity.Service;

var aoExistingItems = oLookup.Items();
if ((aoExistingItems.length === 0) || (oService.EntityId != aoExistingItems[0].id))
{
_bFromSchedulingDialog = true;
oLookupItem = new LookupControlItem(oService.EntityId, oService.ObjectTypeCode, oService.DisplayName);
oLookupItem.data = "";
aoItems = new Array();
aoItems.push(oLookupItem);
oLookup.Clear();
oLookup.AddItems(aoItems);
oLookup.RaiseOnChangeEvent();
}

aoItems = new Array();
oLookup = document.getElementById("resources");
for (i = 0; i < oActivity.Resources.length; i++)
{
oAttendee = oActivity.Resources[i];
oLookupItem = new LookupControlItem(oAttendee.EntityId, oAttendee.ObjectTypeCode, oAttendee.DisplayName);
oLookupItem.data = "";
oLookupItem.resourceSpecId = oAttendee.ResourceSpecId;
oLookupItem.effort = oAttendee.EffortRequired;
aoItems.push(oLookupItem);
}

if (oLookup.AreValuesDifferent(aoItems))
{
oLookup.Clear();
oLookup.AddItems(aoItems);
oLookup.RaiseOnChangeEvent();
}

aoItems = new Array();
oLookup = document.getElementById("customers");
if (oLookup != null)
{
for (i = 0; i < oActivity.Customers.length; i++)
{
oAttendee = oActivity.Customers[i];
oLookupItem = new LookupControlItem(oAttendee.EntityId, oAttendee.ObjectTypeCode, oAttendee.DisplayName);
oLookupItem.data = "";
aoItems.push(oLookupItem);
}

if (aoItems.length == 0)
{

aoItems = null;
}
if (oLookup.AreValuesDifferent(aoItems))
{
oLookup.Clear();
oLookup.AddItems(aoItems);
oLookup.RaiseOnChangeEvent();
}
}

oLookup = document.getElementById("siteid");
if (oLookup != null)
{

oSite = oActivity.Site;
if (oSite != null)
{

var aoExistingItems = oLookup.Items();
if ((aoExistingItems.length === 0) || (oSite.EntityId != aoExistingItems[0].id))
{
oLookupItem = new LookupControlItem(oSite.EntityId, oSite.ObjectTypeCode, oSite.DisplayName);
oLookupItem.data = "";
aoItems = new Array();
aoItems.push(oLookupItem);
oLookup.Clear();
oLookup.AddItems(aoItems);
oLookup.RaiseOnChangeEvent();
}
}
else
{
oLookup.Clear();
oLookup.RaiseOnChangeEvent();
}
}

if (crmForm.isalldayevent.checked)
{
crmForm.isalldayevent.checked = false;
setAllDay();
crmForm.isalldayevent.fireEvent("onchange");
}

var bTimesChanged = false;
if (scheduledstart.DataValue != oActivity.ScheduledStart)
{
scheduledstart.DataValue = oActivity.ScheduledStart;
bTimesChanged = true;
}

if (scheduledend.DataValue != oActivity.ScheduledEnd)
{
scheduledend.DataValue = oActivity.ScheduledEnd;
bTimesChanged = true;
}


if (bTimesChanged)
{
endTimeChanged();
}
}
function ServiceLookupChanged(oEvent)
{

var aoItems = crmForm.serviceid.Items();

if (aoItems.length >= 1)
{
var columns = new Array();
columns.type = "string";
columns[0] = "duration";
columns[1] = "initialstatuscode";
_oServiceRetrieveCommand.SetParameter("id", aoItems[0].id);
_oServiceRetrieveCommand.SetParameter("columns", columns);
_oServiceRetrieveCommand.Execute(HandleGetService);
}

var oResourcesLookup = document.getElementById("resources");
aoItems = oResourcesLookup.Items();
var oItem;
for (i = 0; i < aoItems.length; i++)
{
oItem = aoItems[i];
oItem.resourceSpecId = null;
oItem.effort = null;
oResourcesLookup.UpdateItem(oItem.id, oItem);
}
}
function HandleGetService(oResult)
{
if (oResult.Success)
{
oService = oResult.ReturnValue.service;

if (crmForm.statuscode != null)
{
var oldValue = crmForm.statuscode.DataValue;
crmForm.statuscode.DataValue = oService.initialstatuscode;

if (crmForm.statuscode.DataValue === null)
{
crmForm.statuscode.DataValue = oldValue;
}
}
if (!_bFromSchedulingDialog)
{
crmForm.scheduleddurationminutes.DataValue = oService.duration;

durationChanged();
}

_bFromSchedulingDialog = false;
}
}
